package com.atguigu04.compare.comparator;

import com.atguigu04.compare.Product;

import java.util.Arrays;
import java.util.Comparator;

/**
 * ClassName: comoaratorTest
 * Package: com.atguigu04.compare.comparator
 * Description:
 *
 * @Author BuTang
 * @Create 2023/5/25 18:46
 * @Version 1.0
 */
public class comoaratorTest {
    public static void main(String[] args) {

        /**
         *
         */
        Comparator c = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 == o2 )return 0;
                if (o1 instanceof Product && o2 instanceof Product){
                    Product p1 = (Product) o1;
                    Product p2 = (Product) o2;

                    return -Double.compare(((Product) o1).getPrice(),((Product) o2).getPrice());
                } throw new RuntimeException("数据类型错误");
            }
        };

        Product[] a = new Product[5];
        a[0] = new Product("HuaweiMate50pro",6299);
        a[1] = new Product("Xiaomi13pro",4999);
        a[2] = new Product("VivoX90pro",5999);
        a[3] = new Product("Iphone14ProMax",9999);
        a[4] = new Product("HonorMagic4",6299);

        Arrays.sort(a,c);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i].getPrice()+a[i].getName());
        }
    }
}
